package com.lemon.smartsafetynew.ui.adapter.contact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.ImgLoadUtil;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;
import com.lemon.smartsafetynew.entity.contact.PersonVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class SortAdapter extends RecyclerView.Adapter<SortAdapter.ViewHolder> {

    private RecyclerTClickListener<PersonVo> mListener;
    private LayoutInflater mInflater;
    private List<PersonVo> mData;
    private Context mContext;

    public SortAdapter(Context context, List<PersonVo> data, RecyclerTClickListener<PersonVo> mListener) {
        mInflater = LayoutInflater.from(context);
        mData = data;
        this.mContext = context;
        this.mListener = mListener;
    }

    @Override
    public SortAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.contact_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.tvTag = (TextView) view.findViewById(R.id.tag);
        viewHolder.tvUserName = (TextView) view.findViewById(R.id.name_tv);
        viewHolder.tvDepartment = (TextView) view.findViewById(R.id.department_tv);
        viewHolder.userIconImageView = (CircleImageView) view.findViewById(R.id.user_icon_imageView);
        viewHolder.layout = (LinearLayout) view.findViewById(R.id.layout);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final SortAdapter.ViewHolder holder, final int position) {
        int section = getSectionForPosition(position);
        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            holder.tvTag.setVisibility(View.VISIBLE);
            holder.tvTag.setText(mData.get(position).getLetters());
        } else {
            holder.tvTag.setVisibility(View.GONE);
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });

        }

        //Log.e("yxy", "position = " + position);
        //Log.e("yxy", "this.mData.get(position).getPhotoURL() = " + this.mData.get(position).getPhotoURL());

        holder.tvUserName.setText(CommonUtil.avoidNullString(this.mData.get(position).getUser_name()));
        holder.tvDepartment.setText(CommonUtil.avoidNullString(this.mData.get(position).getDepartment_name()));

        if (!com.lemon.smartsafety.utils.CommonUtil.strIsEmpty(this.mData.get(position).getPhotoURL())) {
            try {
                GlideImageLoader.loadImage(holder.userIconImageView.getContext(), HttpUtils.formatFilesUrl(this.mData.get(position).getPhotoURL()), holder.userIconImageView);
                //ImgLoadUtil.setImgPath(HttpUtils.formatFilesUrl(this.mData.get(position).getPhotoURL()), holder.userIconImageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            holder.userIconImageView.setImageResource(R.mipmap.img_placeholder);
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onItemClick(mData.get(position), position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    //**********************itemClick************************
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    //**************************************************************

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTag;

        CircleImageView userIconImageView;
        TextView tvUserName;
        TextView tvDepartment;
        LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 提供给Activity刷新数据
     *
     * @param list
     */
    public void updateList(List<PersonVo> list) {
        this.mData = list;
        notifyDataSetChanged();
    }

    public Object getItem(int position) {
        return mData.get(position);
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return mData.get(position).getLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = mData.get(i).getLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

}
